import 'package:flutter/material.dart';
import 'package:study_flutter/yzhg/dartTools/UrlLaunchTools.dart';
import 'package:url_launcher/url_launcher.dart';

/*
 * 插件地址： https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
 */
class UrlLauncher extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("插件Url的使用"),
      ),
      body: Container(
        child: Column(
          children: <Widget>[UrlPhone()],
        ),
      ),
    );
  }
}

/*
* 拨打电话
* */
class UrlPhone extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new UrlPhoneState();
  }
}

class UrlPhoneState extends State<UrlPhone> {
  TextEditingController _phoneController = TextEditingController();

  TextEditingController _httpController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          _inputView(
              _phoneController,
              Icon(
                Icons.phone,
                size: 16.0,
              ),
              TextInputType.phone,
              "请输入要拨打的电话"),
          _buttonView(() {
            var phone = _phoneController.text;
            UrlLaunchTools.callPhone(phone);
          }, "拨打电话"),
          _buttonView(() {
            var sms = _phoneController.text;
            UrlLaunchTools.putSms(sms);
          }, "发送短信"),
          _inputView(
              _httpController,
              Icon(
                Icons.sms,
                size: 16.0,
              ),
              TextInputType.text,
              "输入要打开的网址"),
          _buttonView(() {
            var httpUrl = _httpController.text;
            UrlLaunchTools.openHttp(httpUrl);
          }, "打开网址"),
        ],
      ),
    );
  }

  Widget _inputView(TextEditingController controller, Icon icons,
      TextInputType keyboardType, String hintText) {
    return Padding(
      padding: EdgeInsets.all(20.0),
      child: DecoratedBox(
        decoration: BoxDecoration(
          //设置背景色为白色
          color: Colors.white,
          //设置边框
          border: Border.all(
              color: Colors.blueAccent, style: BorderStyle.solid, width: 1.0),
          //设置边框圆角
          borderRadius: BorderRadius.all(Radius.circular(30)),
        ),
        child: TextField(
          autofocus: false,
          //设置弹出数字键盘
          keyboardType: keyboardType,
          controller: controller,
          textAlign: TextAlign.left,
          style: TextStyle(fontSize: 14.0),
          decoration: InputDecoration(
              //去除下划线
              border: InputBorder.none,
              // labelText: '手机号',
              hintText: hintText,
              prefixIcon: icons),
        ),
      ),
    );
  }

  Widget _buttonView(VoidCallback callback, String butMsg) {
    return Padding(
        padding: EdgeInsets.fromLTRB(40.0, 0.0, 40.0, 0.0),
        child: Container(
          width: double.infinity,
          child: FlatButton(
              onPressed: callback,
              color: Colors.blue,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(20.0)),
              child: Text(
                butMsg,
                style: TextStyle(fontSize: 14.01, color: Colors.white),
              )),
        ));
  }
}
